<template>
  <div class="flex-row">
    <el-select v-model="pCode" ref="province" @change="changeProvince" placeholder="省" class="flex-all" style="width: 32%;" v-validate="'required'" name="companyNature">
      <el-option
        v-for="(val, key) in province"
        :filterable="true"
        :key="key"
        :label="val"
        :value="toInt(key)">
      </el-option>
    </el-select>
    <el-select v-model="cCode" placeholder="市" @change="changeCity" ref="city" class="flex-all" style="width: 32%;" v-validate="'required'" name="companyNature">
      <el-option
        v-for="(val, key) in city"
        :filterable="true"
        :key="key"
        :label="val"
        :value="toInt(key)">
      </el-option>
    </el-select>
    <el-select v-model="aCode" placeholder="区/县" ref="area" @change="changeArea" class="flex-all" style="width: 32%;" v-validate="'required'" name="companyNature">
      <el-option
        v-for="(val, key) in area"
        :filterable="true"
        :key="key"
        :label="val"
        :value="toInt(key)">
      </el-option>
    </el-select>
  </div>
</template>

<script>
import pca from '@/libs/pca-code'
export default {
  name: "city-cascader",
  title: "城市级联",
  props: {
    provinceCode: {},
    cityCode: {},
    areaCode: {},
    value: {}
  },
  data () {
    return {
      str: '',
      pCode: '',
      cCode: '',
      aCode: ''
    }
  },
  mounted () {
    this.str = this.value
    this.pCode = this.provinceCode
    this.cCode = this.cityCode
    this.aCode = this.areaCode

  },
  computed: {
    province () {
      return pca[86]
    },
    city () {
      return this.province && this.pCode > 0 ? pca[this.pCode] : {}
      // return this.province && this.pCode.length > 0 ? this.province.find(item => item.code === this.pCode).children : []
    },
    area () {
      // debugger
      if (this.aCode) {
        this.changeArea(this.aCode)
      }
      return this.city && this.cCode > 0 ? pca[this.cCode] : {}
      // return this.city && this.cCode.length > 0 ? this.city.find(item => item.code === this.cCode).children : []
    },
  },
  methods: {
    toInt (num) {
      return parseInt(num)
    },
    changeProvince(val) {
      // 省重新选择将市，县都清空
      // debugger
      this.cCode = ''
      this.aCode = ''
      this.$emit('update:provinceCode', val)
      this.$emit('update:cityCode', '')
      this.$emit('update:areaCode', '')

      this.$nextTick(() => {
        this.str = `${this.$refs.province.selectedLabel} `
        this.$emit('input', this.str)
      })
    },
    changeCity (val) {
      // 市重新选择将县清空
      this.aCode = ''
      this.$emit('update:cityCode', val)
      this.$nextTick(() => {
        this.str = `${this.$refs.province.selectedLabel} ${this.$refs.city.selectedLabel} `
        this.$emit('input', this.str)
      })
      
    },
    changeArea (val) {
      this.$emit('update:areaCode', val)
      this.str = `${this.$refs.province.selectedLabel} ${this.$refs.city.selectedLabel} ${this.$refs.area.selectedLabel}`
      this.$emit('input', this.str)
    },
    clearAll () {
      this.pCode = ''
      this.cCode = ''
      this.aCode = ''
    }
  },
  watch: {
    'provinceCode' (val) {
      this.pCode = val
    },
    'cityCode' (val) {
      this.cCode = val
    },
    'areaCode' (val) {
      this.aCode = val
    }
  }
}
</script>
